/**
Nectarine Utility Project is a software interface for the web system 'demovibes'
Copyright (C) 2010  Alexandre LAURENT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

website: http://code.google.com/p/nectarine-utility-project/
e-mail: lw.demoscene@gmail.com
**/

#include "artistinfodialog.h"

#include <QDialog>
#include <QWidget>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QTextEdit>
#include <QListWidget>
#include <QPushButton>
#include <QFrame>

#include <QDebug>

#include "../Types/artist.h"

#include "utils.h"
#include "../globals.h"

ArtistInfoDialog :: ArtistInfoDialog(const Artist& a, QWidget *parent/*=0*/)
    :QDialog(parent)
{
    unsigned int groupCounter = 0;

    this->setWindowTitle(tr("NUP : Artist %1 viewer").arg(a.id));
    this->setWindowIcon(QIcon(ICONSDIR + QString("user_green.png")));

    pMainGrid = new QVBoxLayout(this);

    pTitleLayout = new QHBoxLayout();
    pTitleLayout->setAlignment(Qt::AlignHCenter);

    pFlagLabel = new QLabel(this);
    pFlagLabel->setPixmap(QPixmap(Utils::getFlagPathFromName(a.flag)));
    pFlagLabel->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);

    pTitleLabel = new QLabel(QString("<b>") + a.nickname + QString("</b>"),this);

    if ( !a.realname.isEmpty() )
    {
        pRealNameLabel = new QLabel(tr("Name: ") + a.realname,this);
    }
    else
    {
        pRealNameLabel = NULL;
    }

    if ( !a.groups.isEmpty() )
    {
        pGroupsLayout = new QGridLayout();

        pGroupsLayout->addWidget(new QLabel(tr("Groups:"),this),0,0);
        for ( QList<Group>::const_iterator itGroup = a.groups.begin() ; itGroup != a.groups.end() ; itGroup++ , groupCounter++ )
        {
            QLabel* pGroupLabel = new QLabel(itGroup->name,this);

            pGroupsLayout->addWidget(pGroupLabel,groupCounter,1);
        }
    }
    else
    {
        pGroupsLayout = NULL;
    }

    if ( !a.location.isEmpty() && a.location != " " )
    {
        pTownLabel = new QLabel(tr("Town: ") + a.location, this);
    }
    else
    {
        pTownLabel = NULL;
    }

    pHLine1 = new QFrame(this);
    pHLine1->setFrameStyle(QFrame::HLine);
    pHLine1->setLineWidth(1);

    pInfoLayout = new QVBoxLayout();
    pInfoLabel = new QLabel(tr("<b>Artist info</b>"),this);

    if ( !a.website.isEmpty() )
    {
        pWebsiteLabel = new QLabel(QString("<a href=\"") + a.website.toString() + QString("\">") + a.website.toString() + QString("</a>"),this);
        pWebsiteLabel->setOpenExternalLinks(true);
    }
    else
    {
        pWebsiteLabel = NULL;
    }

    if ( !a.twitter.isEmpty() )
    {
        // ToDO ... certainly creating a link
    }
    else
    {
        pTwitterLabel = NULL;
    }

    if ( !a.lastfm.isEmpty() )
    {
        // ToDO ... certainly creating a link
    }
    else
    {
        pLastFMLabel = NULL;
    }

    pStatusLabel = new QLabel(tr("Status: ") + statusToString(a.status));
    if ( !a.aliasOf.isEmpty())
    {
        pAliasOfLabel = new QLabel(QString(tr("Alias of: ")) + a.aliasOf,this);
    }
    else
    {
        pAliasOfLabel = NULL;
    }

    if ( a.aliases.size() != 0 )
    {
        bool start = true;
        pAliasesLabel = new QLabel(tr("Aliases: "));
        for ( QList<QString>::const_iterator itAliases = a.aliases.begin() ; itAliases != a.aliases.end() ; ++itAliases )
        {
            if ( !start )
            {
                pAliasesLabel->setText(pAliasesLabel->text() + QString(", "));
            }
            pAliasesLabel->setText(pAliasesLabel->text() + *itAliases);
            start=false;
        }
    }
    else
    {
        pAliasesLabel = NULL;
    }

    if ( !a.info.isEmpty() )
    {
        pAdditionnalInfoViewer = new QTextEdit(this);
        pAdditionnalInfoViewer->setReadOnly(true);
        {
            QString infoString = a.info;
            infoString.prepend("<b>Additionnal info:</b><br/>");

            pAdditionnalInfoViewer->setHtml(infoString);;
        }
        pAdditionnalInfoViewer->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Maximum);
    }
    else
    {
        pAdditionnalInfoViewer = NULL;
    }

    pHLine2 = new QFrame(this);
    pHLine2->setFrameStyle(QFrame::HLine);
    pHLine2->setLineWidth(1);

    pSongsListViewer = new QListWidget(this);

    for ( QList<Song>::const_iterator itSong = a.songs.begin() ; itSong != a.songs.end() ; ++itSong )
    {
        pSongsListViewer->addItem(itSong->title);
    }

    pHLine3 = new QFrame(this);
    pHLine3->setFrameStyle(QFrame::HLine);
    pHLine3->setLineWidth(1);

    pValidateButton = new QPushButton(tr("Ok"),this);

    pMainGrid->addLayout(pTitleLayout);
    pTitleLayout->addWidget(pFlagLabel);
    pTitleLayout->addWidget(pTitleLabel);
    if ( pRealNameLabel )
    {
        pMainGrid->addWidget(pRealNameLabel,0,Qt::AlignHCenter);
    }
    if ( pGroupsLayout )
    {
        pMainGrid->addLayout(pGroupsLayout);
    }
    if ( pTownLabel )
    {
        pMainGrid->addWidget(pTownLabel,0,Qt::AlignHCenter);
    }
    pMainGrid->addWidget(pHLine1);
    pMainGrid->addLayout(pInfoLayout);
        pInfoLayout->addWidget(pInfoLabel,0,Qt::AlignHCenter);
        if ( pWebsiteLabel != NULL )
        {
            pInfoLayout->addWidget(pWebsiteLabel,0,Qt::AlignHCenter);
        }
        if ( pTwitterLabel != NULL )
        {
            pInfoLayout->addWidget(pTwitterLabel,0,Qt::AlignHCenter);
        }
        if ( pLastFMLabel != NULL )
        {
            pInfoLayout->addWidget(pLastFMLabel,0,Qt::AlignHCenter);
        }
        pInfoLayout->addWidget(pStatusLabel,0,Qt::AlignHCenter);
        if ( pAliasOfLabel != NULL )
        {
            pInfoLayout->addWidget(pAliasOfLabel,0,Qt::AlignHCenter);
        }
        if ( a.aliases.size() != 0 )
        {
            pInfoLayout->addWidget(pAliasesLabel,0,Qt::AlignHCenter);
        }
        if ( pAdditionnalInfoViewer != NULL )
        {
            pInfoLayout->addWidget(pAdditionnalInfoViewer,0,Qt::AlignHCenter);
        }
    pMainGrid->addWidget(pHLine2);
    pMainGrid->addWidget(pSongsListViewer);
    pMainGrid->addWidget(pHLine3);
    pMainGrid->addWidget(pValidateButton);

    connect(pValidateButton, SIGNAL(clicked()), this, SLOT(accept()));

    qDebug() << "ArtistInfoDialog created";
}

ArtistInfoDialog :: ~ArtistInfoDialog(void)
{
    qDebug() << "ArtistInfoDialog deleted";
}

void ArtistInfoDialog :: accept(void)
{
    qDebug() << "ArtistInfoDialog :: accept";

    this->done(QDialog::Accepted);
    emit deleteAIDSignal(this);
}

void ArtistInfoDialog :: reject(void)
{
    qDebug() << "ArtistInfoDialog :: reject";

    this->done(QDialog::Rejected);
    emit deleteAIDSignal(this);
}
